package com.unbank.bhi.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.WebDriver;
import org.w3c.dom.NodeList;

import com.unbank.bhi.BhiXmlUtil;
import com.unbank.bhi.dao.BhiDaoImpl;

public class BhiAnalyze {
	private static Log logger = LogFactory.getLog(BhiAnalyze.class);

	public void insertbhi(WebDriver webDriver) {
		Document frontPageDoc = Jsoup.parse(webDriver.getPageSource());
		org.w3c.dom.Document doc = BhiXmlUtil.getDocument();
		NodeList nl = BhiXmlUtil.getNodeList();// 从配置文件中读取到的内容封装成NodeList
		StringBuffer content = new StringBuffer("insert into grab_bhi (");
		StringBuffer value = new StringBuffer(") values (");
		for (int i = 0; i < nl.getLength(); i++) {// 遍历节点list

			String column = doc.getElementsByTagName("column").item(i)
					.getFirstChild().getNodeValue(); // 字段名

			// String contentreg =
			// doc.getElementsByTagName("contentreg").item(i)
			// .getFirstChild().getNodeValue();// 字段的节点位置内容

			String sxpath = doc.getElementsByTagName("sxpath").item(i)
					.getFirstChild().getNodeValue();// 字段的节点位置内容
			logger.info(sxpath);
			content.append(column + ",");// sql的拼接
			org.jsoup.nodes.Element frontNode = null;
			String p_value = null;

			try {
				frontNode = ((org.jsoup.nodes.Element) frontPageDoc).select(
						sxpath).get(0);
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			if (null != frontNode && !column.equalsIgnoreCase("")) {
				p_value = frontNode.text();
			}

			if (i == 21) {
				int k = 16;
				while (null != p_value && !p_value.endsWith("邮 编") && k <= 18) {

					try {
						frontNode = ((org.jsoup.nodes.Element) frontPageDoc)
								.select("html body div div.header div#content form#myform div.leftColumn table.infoTable tbody tr:eq("
										+ k + ") td:eq(0)").get(0);
					} catch (Exception e1) {
						e1.printStackTrace();
					}

					if (null != frontNode && !column.equalsIgnoreCase("")) {
						p_value = frontNode.text();
					}
					k++;
				}
				/***************/
				try {
					frontNode = ((org.jsoup.nodes.Element) frontPageDoc)
							.select("html body div div.header div#content form#myform div.leftColumn table.infoTable tbody tr:eq("
									+ (k - 1) + ") td:eq(1)").get(0);
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				if (null != frontNode && !column.equalsIgnoreCase("")) {
					p_value = frontNode.text();
				}
			}

			if (i == 22) {
				int k = 17;
				while (null != p_value && !p_value.endsWith("详细地址") && k <= 20) {

					try {
						frontNode = ((org.jsoup.nodes.Element) frontPageDoc)
								.select("html body div div.header div#content form#myform div.leftColumn table.infoTable tbody tr:eq("
										+ k + ") td:eq(0)").get(0);
					} catch (Exception e1) {
						e1.printStackTrace();
					}

					if (null != frontNode && !column.equalsIgnoreCase("")) {
						p_value = frontNode.text();
					}
					k++;
				}
				/***************/
				try {
					frontNode = ((org.jsoup.nodes.Element) frontPageDoc)
							.select("html body div div.header div#content form#myform div.leftColumn table.infoTable tbody tr:eq("
									+ (k - 1) + ") td:eq(1)").get(0);
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				if (null != frontNode && !column.equalsIgnoreCase("")) {
					p_value = frontNode.text();
				}
			}

			if (i == 23) {
				int k = 17;
				try {
					frontNode = ((org.jsoup.nodes.Element) frontPageDoc)
							.select("html body div div.header div#content form#myform div.leftColumn table.infoTable tbody tr:eq("
									+ k + ")  th:eq(0)").get(0);
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				if (null != frontNode && !column.equalsIgnoreCase("")) {
					p_value = frontNode.text();
				}

				while (null != p_value && !p_value.endsWith("项目简介")) {

					try {
						frontNode = ((org.jsoup.nodes.Element) frontPageDoc)
								.select("html body div div.header div#content form#myform div.leftColumn table.infoTable tbody tr:eq("
										+ k + ")  th:eq(0)").get(0);
					} catch (Exception e1) {
						e1.printStackTrace();
					}

					if (null != frontNode && !column.equalsIgnoreCase("")) {
						p_value = frontNode.text();
					}
					k++;
				}
				/***************/
				if (p_value.endsWith("项目简介")) {
					k = k + 1;
				}
				try {
					frontNode = ((org.jsoup.nodes.Element) frontPageDoc)
							.select("html body div div.header div#content form#myform div.leftColumn table.infoTable tbody tr:eq("
									+ k + ") td:eq(0)").get(0);
				} catch (Exception e1) {
					e1.printStackTrace();
				}

				if (null != frontNode && !column.equalsIgnoreCase("")) {
					p_value = frontNode.text();
				}
			}

			logger.info(column + "  " + p_value + "  " + sxpath);
			/***************/

			if ("pro_intro".equals(column)) {
				p_value = p_value.replaceAll("&nbsp;", "");
			}
			value.append("'" + p_value + "',");// sql的拼接
			// if ("pro_time".equals(column)) {// 判断抓取到的内容是否在所需的时间段内
			// String datestr = p_value;
			// SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			// "yyyy-MM-dd");
			// // Date begindate = simpleDateFormat.parse(begintime);
			// // Date enddate = simpleDateFormat.parse(endtime);
			// Date date = simpleDateFormat.parse(datestr);
			// if (date.getTime() < begindate.getTime()
			// || date.getTime() > enddate.getTime()) {
			// return;
			// }
			// }
			// if ("pro_name".equals(column)) {// 判断抓取的内容是否已经抓取过
			// for (int i = 0; i < list.size(); i++) {
			// String name = list.get(i).get("pro_name").toString();
			// if (name.equals(p_value)) {
			// return;
			// }
			// }
			// }
		}
		String cont = content.substring(0, content.length() - 1);
		String val = value.substring(0, value.length() - 1);
		String sql = cont + val.toString() + ")";// 组装成完整Sql
		BhiDaoImpl.executeUpdate(sql);// 插入数据库
	}

}
